package com.scwang.smart.drawer.exercise.adapter

import android.graphics.Color
import android.view.View
import android.widget.TextView
import androidx.annotation.IdRes
import androidx.recyclerview.widget.RecyclerView
import com.scwang.smart.drawer.exercise.model.Subject
import java.util.regex.Matcher
import java.util.regex.Pattern

abstract class ExerciseHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    abstract fun onBind(subject: Subject, position: Int)

    protected fun bindTag(@IdRes tagId: Int, con: String?): String? {
        var content: String = con ?: return con

        val matcherTag: Matcher = mPatternTag.matcher(content)
        if (matcherTag.find()) {
            var color = -0x1
            val back: Int = parseColor(matcherTag.group(1), -0x6700)
            val findTag = matcherTag.group()
            val matcherColor =
                Pattern.compile("(?<!-)color:([#\\d\\w]+)").matcher(findTag)
            if (matcherColor.find()) {
                color = parseColor( matcherColor.group(1), color)
            }

            val tagText = itemView.findViewById<TextView>(tagId)
            tagText.visibility = View.VISIBLE
            tagText.text = matcherTag.group(2)
            tagText.setBackgroundColor(back)
            tagText.setTextColor(color)

            if (!findTag.contains("<font color='#ffffff'")) {
                val replace = findTag
                    .replace("<span", "<font color='#ffffff'")
                    .replace("</span>", "</font>")
                content = content.replace(findTag, replace)
            }
        } else {
            itemView.findViewById<TextView>(tagId).visibility = View.GONE
        }
        return content
    }

    companion object {
        
        @JvmStatic
        protected val mPatternItem: Pattern =
            Pattern.compile("^(?:<p>)?([^<]+)(?:</p>)?$")
        
        @JvmStatic
        protected val mPatternSubject: Pattern =
            Pattern.compile("^(?:<p>)?((?:<span.+?>[^<]+</span>)?(?:&nbsp;)?[^<]+)(?:</p>)?$")

        @JvmStatic
        protected val mPatternTag: Pattern =
            Pattern.compile("^(?:<p>)?<span.+?background-color:([#\\d\\w]+).+?>(\\S{2,4}?)</span>\\s*&nbsp;")

        @JvmStatic
        protected fun matcherHtmlText(pattern: Pattern, htmlString: String ): Matcher {
            var html = htmlString
            html = html.replace(
                ("" +
                        " ?margin:\\s*8px\\s+0px;?" +
                        "| ?padding:\\s*0px;?" +
                        "| ?line-height:\\s*1.75em;?" +
                        "| ?font-family:\\s*微软雅黑;?" +
                        "| ?font-size:\\s*16px;?" +
                        "|<br/>$").toRegex(), ""
            )
            html = html.replace(" ?style=['\"]\\s*['\"] ?".toRegex(), "")
            html = html.replace("<span>([^<]*)</span>".toRegex(), "$1")
            html = html.replace("<br/></p>".toRegex(), "</p>")
            html = html.replace("<br/>".toRegex(), "\n")
            html = html.replace("<p>([^<]*)</p>$".toRegex(), "$1")
            html = html.replace("<p>([^<]*)</p>".toRegex(), "$1\n")
            //防止 p span 多重嵌套
            html = html.replace("<span>([^<]*)</span>".toRegex(), "$1")
            html = html.replace("<p>([^<]*)</p>$".toRegex(), "$1")
            html = html.replace("<p>([^<]*)</p>".toRegex(), "$1\n")
            return pattern.matcher(html)
        }

        @JvmStatic
        protected fun parseColor(colorStr: String?, defaultColor: Int): Int {
            var colorString: String = colorStr ?: return defaultColor
            if (colorString[0] == '#' && colorString.length == 4) {
                val builder = StringBuilder()
                for (i in colorString.indices) {
                    builder.append(colorString[i])
                    if (i > 0) {
                        builder.append(colorString.get(i))
                    }
                }
                colorString = builder.toString()
            }
            return try {
                Color.parseColor(colorString)
            } catch (e: Exception) {
                defaultColor
            }
        }
    }
}